package net.sos.translator.client;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import net.sos.translator.CollectionTranslatorImpl;
import net.sos.translator.GoogleTranslatorImpl;
import net.sos.translator.ICollectionTranslator;
import net.sos.translator.ILocaleTranslator;
import net.sos.translator.ITextFileTranslator;
import net.sos.translator.PropertiesFileTranslatorImpl;
import net.sos.translator.TextFileTranslatorImpl;
import net.sos.translator.YahooTranslatorImpl;

import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPException;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.UnflaggedOption;

public class TranslatorClient {

   public static void main(final String[] args) {
      JSAPResult result = getParsedArgs(args);

      Locale fromLocale, toLocale;
      fromLocale = new Locale(result.getString("fromLocale"));
      toLocale = new Locale(result.getString("toLocale"));

      ILocaleTranslator translator;
      String impl = result.getString("implementation");
      if ("google".equals(impl)) {
         translator = new GoogleTranslatorImpl();
      } else if("yahoo".equals(impl)) {
         translator = new YahooTranslatorImpl();
      } else {
         throw new RuntimeException("Unknown translator provider specified: " + impl);
      }
      translator.setFromLocale(fromLocale);
      translator.setToLocale(toLocale);

      String srcPath, destPathTxt, destPathProp;
      srcPath = result.getString("srcFile");
      destPathTxt = result.getString("destFileTxt");
      destPathProp = result.getString("destFileProp");
      if (destPathTxt == null && destPathProp == null) {
         // Since none of the files are specified means that the
         // user is trying to translate words specified at the
         // command line; use the ICollectionTranslator.
         ICollectionTranslator collectionTranslator = new CollectionTranslatorImpl();
         collectionTranslator.setTranslator(translator);
         List<CharSequence> list = new ArrayList<CharSequence>();
         list.addAll(Arrays.asList(result.getStringArray("words")));
         System.out.println(collectionTranslator.translate(list));
      } else {
         // The user either wants to translate a text file or a properties file
         File srcFile = new File(srcPath);
         if (destPathTxt != null) {
            File destFile = new File(destPathTxt);
            ITextFileTranslator textFileTranslator = new TextFileTranslatorImpl();
            textFileTranslator.setTranslator(translator);
            textFileTranslator.translateFile(srcFile, destFile);
         }
         if (destPathProp != null) {
            File destFile = new File(destPathProp);
            ITextFileTranslator textFileTranslator = new PropertiesFileTranslatorImpl();
            textFileTranslator.setTranslator(translator);
            textFileTranslator.translateFile(srcFile, destFile);
         }
      }

   }

   private static JSAPResult getParsedArgs(final String[] args) {
      JSAP jsap = new JSAP();
      try {
         jsap.registerParameter(new FlaggedOption("implementation").setDefault(
               "yahoo").setShortFlag('i').setLongFlag("impl"));
         jsap.registerParameter(new FlaggedOption("fromLocale").setShortFlag(
               'f').setLongFlag("from-locale").setRequired(true));
         jsap.registerParameter(new FlaggedOption("toLocale").setShortFlag('t')
               .setLongFlag("to-locale").setRequired(true));
         jsap.registerParameter(new FlaggedOption("srcFile").setShortFlag('s')
               .setLongFlag("source-file"));
         jsap.registerParameter(new FlaggedOption("destFileTxt").setShortFlag(
               'x').setLongFlag("dest-file-text"));
         jsap.registerParameter(new FlaggedOption("destFileProp").setShortFlag(
               'p').setLongFlag("dest-file-properties"));
         jsap.registerParameter(new UnflaggedOption("words").setStringParser(
               JSAP.STRING_PARSER).setGreedy(true).setRequired(false));
         return jsap.parse(args);
      } catch (JSAPException e) {
         e.printStackTrace();
      }
      return null;
   }

   // public static void main(final String[] args) throws Exception {
   // System.setProperty("org.apache.commons.logging.Log",
   // "org.apache.commons.logging.impl.SimpleLog");
   // System.setProperty("org.apache.commons.logging.simplelog.showdatetime",
   // "true");
   // System
   // .setProperty(
   // "org.apache.commons.logging.simplelog.log.httpclient.wire",
   // "debug");
   // System
   // .setProperty(
   // "org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient",
   // "debug");
   // propertiesTranslator();
   // }

}
